//
//  DevciceChartCell.m
//  efairy
//
//  Created by 李锋 on 2019/1/21.
//  Copyright © 2019年 East. All rights reserved.
//

#import "DevciceChartCell.h"
#import <Highcharts/Highcharts.h>
#import "EFAModel.h"
#import "NSDate+XDTimerFormatter.h"

@interface DevciceChartCell()
@property (nonatomic, strong) NSArray<NSString *> *colorArray;
@end
@implementation DevciceChartCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.colorArray = @[@"7CD89E", @"FFA954", @"629CE6", @"E671D7", @"B28CE5"];
        [self initView];
    }
    return self;
}

-(void)initView{
    _titleView = [[UIView alloc]init];
    _titleImg = [[UIImageView alloc]init];
    _titleImg.image = [UIImage imageNamed:@"icon_label"];
    _titleLable = [[UILabel alloc]init];
    _titleLable.font = [UIFont boldSystemFontOfSize:16.f];
    _titleLable.textColor = ColorFromRGB(0x333333);
    

    _titleLable2 = [[UILabel alloc]init];
    _titleLable2.font = [UIFont boldSystemFontOfSize:11.f];
    _titleLable2.textColor = ColorFromRGB(0x999999);
    _titleLable2.text = @"数据发送频率1分钟/次";
    [_titleView sd_addSubviews:@[_titleImg,_titleLable,_titleLable2]];
    _titleImg.sd_layout.widthIs(6).heightIs(22)
    .topSpaceToView(_titleView, 12)
    .leftSpaceToView(_titleView, 0);
    
    _titleLable.sd_layout.heightIs(16)
    .leftSpaceToView(_titleImg, 6)
    .topSpaceToView(_titleView, 15);
    
    _titleLable2.sd_layout.heightIs(11)
    .rightSpaceToView(_titleView, 12)
    .leftSpaceToView(_titleLable, 6)
    .topSpaceToView(_titleView, 20);
    
    
    
    _chartView = [[HIChartView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,  SCREEN_WIDTH * 700 / 750)];
    [self.contentView sd_addSubviews:@[_titleView,_chartView]];
    _titleView.sd_layout.heightIs(46)
    .widthIs(SCREEN_WIDTH)
    .topSpaceToView(self.contentView, 0);
    
    _chartView.sd_layout.topSpaceToView(_titleView, 0);
    
    
    
    
   [_titleLable setSingleLineAutoResizeWithMaxWidth:(66)];
    [self setupAutoHeightWithBottomView:_chartView bottomMargin:0];
    
}

- (void)configViewWithEFADeviceInfoModel:(EFADeviceInfoModel *)model indexPath:(NSIndexPath *)indexPath {
    if (indexPath.row >= model.streamPtCidArray.count) {
        return;
    }
    EFADevPtCidItem *ptCid = model.streamPtCidArray[indexPath.row];
    NSInteger curPt = ptCid.pt;
    NSDictionary *ptInfoDic = [[EFADeviceProtocolModel sharedInstance] protocolInfoWithPt:curPt];
    
    // HIOptions
    HIOptions *options = [[HIOptions alloc] init];
    // HIChart
    HIChart *chart = [[HIChart alloc] init];
    chart.type = @"spline";
    chart.panning = @(YES);
    //    chart.pinchType = @"xy";
    chart.zoomType = @"x";
    options.chart = chart;
    //    // HITooltip(配合手势)
    //    HITooltip *toolTip = [[HITooltip alloc] init];
    //    toolTip.followTouchMove = @(NO);
    //    options.tooltip = toolTip;
    HITitle *title = [[HITitle alloc]init];
    title.text = @"";
    title.align = @"left";
    title.style = ({
        HIStyle *style = [[HIStyle alloc] init];
        style.color = @"#222222";
        style.fontSize = @"0px";
        style;
    });
    options.title = title;
     _titleLable.text = ptInfoDic[EFADeviceProtocolPtTitle];
    // HIExporting
    options.exporting = ({
        HIExporting *exporting = [[HIExporting alloc] init];
        exporting.enabled = @(NO);
        exporting;
    });
    // HIXAxis
    HIXAxis *xAxis = [[HIXAxis alloc] init];
    xAxis.categories = model.timestampArray;
    options.xAxis = @[xAxis];
    // HIYAxis, HIYAxisTitle
    HIYAxis *yAxis = [[HIYAxis alloc] init];
    //    HIYAxisTitle * yAxisTitle = [[HIYAxisTitle alloc] init];
    //    yAxisTitle.text = [NSString stringWithFormat:@"%@(%@)", ptInfoDic[EFADeviceProtocolPtTitle], ptInfoDic[EFADeviceProtocolPtUnit]];;
    //    yAxis.title =  yAxisTitle;
    yAxis.title = ({
        HITitle *title = [[HITitle alloc] init];
        title.text = [NSString stringWithFormat:@"%@(%@)", ptInfoDic[EFADeviceProtocolPtTitle], ptInfoDic[EFADeviceProtocolPtUnit]];
        title;
    });
    options.yAxis = @[yAxis];
    // HILine(直线) HISpline(曲线)
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:ptCid.cidArray.count];
    [ptCid.cidArray enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HISpline *series = [[HISpline alloc] init];
        series.name = [NSString stringWithFormat:@"%@通道%ld", [ptCid cidTitleWithCid:obj], (long)[ptCid cidDisplayWithCid:obj]];
        series.data = [model rtvArrayWithCid:obj.integerValue pt:ptCid.pt];
        NSInteger colorIndex = idx % self.colorArray.count;
        series.color = [[HIColor alloc] initWithHexValue:self.colorArray[colorIndex]];
        [mArray addObject:series];
    }];
    options.series = mArray;
    // 隐藏Logo
    HICredits *credits = [[HICredits alloc] init];
    credits.enabled = @(NO);
    options.credits = credits;
    // HIGChartView
    self.chartView.options = options;
    //    [self.chartView loadChart];
}
@end
